package cn.mogugo.joke.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.mogugo.comm.BaseAction;
import cn.mogugo.comm.CC;
import cn.mogugo.comm.PageSupport;
import cn.mogugo.joke.action.form.JokeObjForm;
import cn.mogugo.joke.model.JokeCategory;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.joke.model.JokeWeb;
import cn.mogugo.util.tools.DateTools;
import cn.mogugo.util.tools.NumberTools;
import cn.mogugo.util.tools.Page;

public class JokeObj99Action extends BaseAction{
	
	public ActionForward saveOrUpdate99(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		String type = request.getParameter("type");
		
		
		JokeObjForm jokeObjForm = (JokeObjForm)form;
		JokeObj jo;
		if(StringUtils.isNotBlank(jokeObjForm.getId())){
			jo = (JokeObj)this.baseDao.get(JokeObj.class, Integer.valueOf(jokeObjForm.getId()));
		}else{
			jo = new JokeObj();
		}
		BeanUtils.copyProperties(jo , jokeObjForm);
		jo.setJoke_content(jokeObjForm.getJoke_content().replaceAll("\\s+", "<br>"));
		if(jo.getId() == 0){
			jo.setId(null);
			jo.setJoke_date(DateTools.getCurDateTime());
			jo.setAuditing(99);
			JokeWeb jw = (JokeWeb)this.baseDao.get(JokeWeb.class, 99);
			jo.setJokeWeb(jw);
			jo.setJokeCategory((JokeCategory)this.baseDao.get(JokeCategory.class, jokeObjForm.getCategory_id()));
			
		}else{
			if(jokeObjForm.getAuditing() != null){
				jo.setAuditing(jokeObjForm.getAuditing());
			}else{
				jo.setAuditing(Integer.valueOf(type));
			}
			if(jokeObjForm.getCategory_id() != null){
				jo.setJokeCategory((JokeCategory)this.baseDao.get(JokeCategory.class, jokeObjForm.getCategory_id()));
			}
		}
		this.baseDao.saveOrUpdateLog(jo);
		
		return mapping.findForward("tolist99");
	}
	
	
	
	
	public ActionForward list99(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		String strPageNo = request.getParameter("pageNo");
		Integer pageNo = NumberTools.formatObject2IntDefaultParamNoExp(strPageNo, 1);
		
		Map map = new HashMap();
		String strCategoryId = request.getParameter("categoryId");
		if(StringUtils.isNotBlank(strCategoryId)){
			map.put("categoryId", Integer.valueOf(strCategoryId));
		}
		
		JokeObjForm jokeObjForm = (JokeObjForm)form;
		String id = jokeObjForm.getId();
		if(StringUtils.isNotBlank(id)){
			map.put("id", Integer.valueOf(id));
		}
		
		
		String jokeObjTitle = jokeObjForm.getJoke_title();
		if(StringUtils.isNotBlank(jokeObjTitle)){
			map.put("joke_title", jokeObjTitle);
		}
		map.put("auditing", 99);
		
		PageSupport apage = this.jokeObjManager.getPage(map, pageNo, CC.PAGE_SIZE , "new");
		request.setAttribute("page", apage.getItems());
    	request.setAttribute("pageStr", Page.getInstance().createPageStr(request, apage));
    	
    	
		return mapping.findForward("list99");
	}
	
	
	
	public ActionForward create99(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		List listJc = this.jokeCategoryManager.getAll();
		request.setAttribute("listJc", listJc);
		return mapping.findForward("edit99");
	}
	
	

	
	
	public ActionForward edit99(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		JokeObj jo = (JokeObj)this.baseDao.get(JokeObj.class, Integer.valueOf(id));
		JokeObjForm jokeObjForm = (JokeObjForm)form;
		BeanUtils.copyProperties(jokeObjForm ,jo);
		
		if(jo.getJokeCategory() != null){
			jokeObjForm.setCategory_id(jo.getJokeCategory().getId());	
		}
		if(jo.getJokeWeb() != null){
			jokeObjForm.setWeb_id(jo.getJokeWeb().getId());
		}
		jokeObjForm.setJoke_content(jo.getJoke_content().replaceAll("<br>", "\n"));
		request.setAttribute("jo", jo);
		
		List listJc = this.jokeCategoryManager.getAll();
		request.setAttribute("listJc", listJc);
		
		return mapping.findForward("edit99");
	}
	
	
	
	public ActionForward delete99(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		this.baseDao.deleteObjectByIdLog(JokeObj.class, Integer.valueOf(id));
		return mapping.findForward("tolist99");
	}
}
